# coding: utf-8
import logging
import opensocial

from kay.conf import settings

def get_current_user_profile(fcauth, fields=None):
  config = opensocial.ContainerConfig(server_rpc_base=settings.GFC_RPC_URL, security_token=fcauth, security_token_param='fcauth')
  container = opensocial.ContainerContext(config)

  try:
    user = container.fetch_person(fields=fields)
  except:
    logging.exception("Problem getting the viewer")
    user = False

  return user


def create_activity(user_id, title, body):
  config = opensocial.ContainerConfig(server_rpc_base=settings.GFC_RPC_URL, oauth_consumer_key=settings.GFC_API_KEY, oauth_consumer_secret=settings.GFC_API_SECRET)
  container = opensocial.ContainerContext(config)

  activity = opensocial.Activity({})
  activity['title'] = title
  activity['body'] = body

  try:
    request = opensocial.CreateActivityRequest(user_id, activity)
    response = container.send_request(request)
  except:
    logging.exception("Problem sending activity %s" % title)
    raise
